import okhttp3.*;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

public class MockBusiness {
    static int qps = 500;
    static int times = 2000;
    static OkHttpClient client = new OkHttpClient();

    static AtomicInteger counter = new AtomicInteger();

    public static void main(String[] args) {
        doSync();
    }

    private static void doAsync() {
        for (int i = 0; i < times; i++) {
            for (int j = 0; j < qps; j++) {
                client.newCall(buildRequest("dal")).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        System.out.println("dal error" + e.getMessage());
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String dal = response.body() == null ? "" : response.body().string();
                        client.newCall(buildRequest("redis")).enqueue(new Callback() {
                            @Override
                            public void onFailure(Call call, IOException e) {
                                System.out.println("redis error" + e.getMessage());
                            }

                            @Override
                            public void onResponse(Call call, Response response) throws IOException {
                                String redis = response.body() == null ? "" : response.body().string();
                                client.newCall(buildRequest("image")).enqueue(new Callback() {
                                    @Override
                                    public void onFailure(Call call, IOException e) {
                                        System.out.println("image error" + e.getMessage());
                                    }

                                    @Override
                                    public void onResponse(Call call, Response response) throws IOException {
                                        String image = response.body() == null ? "" : response.body().string();
                                        client.newCall(buildRequest("sss")).enqueue(new Callback() {
                                            @Override
                                            public void onFailure(Call call, IOException e) {
                                                System.out.println("sss error" + e.getMessage());
                                            }

                                            @Override
                                            public void onResponse(Call call, Response response) throws IOException {
                                                String sss = response.body() == null ? "" : response.body().string();
                                                String name = Thread.currentThread().getName();

                                                System.out.println(name + ": " + counter.incrementAndGet() + dal + redis + image + sss);

                                            }
                                        });
                                    }
                                });
                            }
                        });
                    }
                });
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private static void doSync() {
//        ExecutorService executorService = Executors.newFixedThreadPool(qps);
//        for (int i = 0; i < qps; i++) {
//            executorService.submit(() -> {
        long start = System.currentTimeMillis();
//                for (int j = 0; j < times; j++) {

        String dal = request("dal");
        System.out.println(System.currentTimeMillis() - start);

        String redis = request("redis");
        System.out.println(System.currentTimeMillis() - start);

        String image = request("image");
        System.out.println(System.currentTimeMillis() - start);

        String sss = request("sss");
        System.out.println(System.currentTimeMillis() - start);

        String name = Thread.currentThread().getName();
        System.out.println(name + ": " + counter.incrementAndGet() + dal + redis + image + sss);

//        try {
//            Thread.sleep(1000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//                }
        System.out.println("cost: " + (System.currentTimeMillis() - start) + "ms");
//            });
//        }
    }

    static String request(String service) {
        Request request = buildRequest(service);
        try {
            return client.newCall(request).execute().body().string();
        } catch (IOException e) {
            return e.getMessage();
        }
    }

    private static Request buildRequest(String service) {
        return new Request.Builder()
                .url("http://localhost:8086/" + service)
                .get()
                .build();
    }


}
